package com.sz.app.oss.web.security;

import com.sz.app.oss.constants.ModuleNames;
import com.sz.biz.app.web.security.AbstractCredentialsMatcher;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.DaoSupport;
import com.sz.common.core.service.PrincipalLogUtils;
import org.apache.shiro.cache.CacheManager;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


public class RetryLimitHashedCredentialsMatcher extends
        AbstractCredentialsMatcher {

    @Resource(name = "daoSupport")
    private DaoSupport dao;


    public RetryLimitHashedCredentialsMatcher(CacheManager cacheManager) {
        super(cacheManager);
    }

    @Override
    protected void lockUser(int accountId, String accountName) {
        List<Integer> accountList = new ArrayList<>();
        accountList.add(accountId);
        dao.update("UserMapper.lockUserByUserIds", accountList);

        passwordRetryCache.remove(accountName);
        PrincipalLogUtils.addOperationLog(ModuleNames.SYS, ModuleNames.SYS_LOGIN, UserActions.LOCK,
                accountName + "输入错误次数过多，被系统锁定");
    }

    @Override
    protected String getPasswordRetryCacheName() {
        return "passwordRetryCache";
    }
}
